package org.oims.simpleserver.config;

import org.oims.simpleserver.filters.AuthenticateFilter;
import org.oims.simpleserver.filters.VuePageRouterFilter;
import org.oims.simpleserver.filters.WebsocketFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class WebFiltersConfig {
    private String[] vuePageRouterUrlPattens;

    public WebFiltersConfig(){
        this.vuePageRouterUrlPattens = new String[]{
                "/login", "/conversationList", "/newMessage", "/addContact", "/chatWindow"
        };
    }
    // 注册Filter
    @Bean
    public FilterRegistrationBean<Filter> registerAuthenticateFilter(){
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new AuthenticateFilter());
        filterRegistrationBean.addUrlPatterns("/api/*");
        filterRegistrationBean.setName("AuthenticateFilter");
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<Filter> registerWebsocketFilter() {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new WebsocketFilter());
        filterRegistrationBean.addUrlPatterns("/wss/topic");
        filterRegistrationBean.setName("wsFilter");
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<Filter> registerVuePageRouterFilter() {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new VuePageRouterFilter());
        filterRegistrationBean.addUrlPatterns(this.vuePageRouterUrlPattens);
        filterRegistrationBean.setName("vuePageRouterFilter");
        return filterRegistrationBean;
    }
}
